package com.hziee.mail.controller;

import com.alibaba.fastjson.JSON;
import com.hziee.mail.model.OrderInfoEntity;
import com.hziee.mail.service.OrderService;
import com.hziee.mail.util.FormatReturn;
import com.hziee.mail.util.OrderCodeFactory;
import com.hziee.mail.util.RedisUtil;
import com.hziee.mail.util.SpringUtil;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author ZX
 */
@RestController
@RequestMapping("/order")
public class OrderController extends FormatReturn {

    @Autowired
    private OrderService orderService;


    @GetMapping("add_order_temporary")
    public String addOrderTemporary(@RequestParam(value = "goodsId[]") String[] goodsId, @RequestParam(value = "value[]") Integer[] value, @RequestParam(value = "price[]") double[] price, Integer userId, HttpServletResponse response) {
        for (int i = 0; i < goodsId.length; i++) {
            orderService.addOrderTemporary(goodsId[i], value[i], price[i], userId);
        }
        RedisUtil redisUtil = (RedisUtil) SpringUtil.applicationContext.getBean("redisUtil");
        redisUtil.delete(userId + "cart");
        return ajaxReturn(response, 1, "success", null);
    }

    @GetMapping("get_order_temporary")
    public String getOrderTemporary(@RequestParam(value = "goodsId[]") String[] goodsId, Integer userId, HttpServletResponse response) {
        System.out.println(goodsId[0] + " " + userId);
        List<List<Map>> list = orderService.getOrderTemporary(goodsId, userId);
        return ajaxReturn(response, 1, "success", list);
    }

    @PostMapping("/add_order_info")
    public String addOrderInfo(String goodsId, Integer userId, double allmoney, Integer orderStatus, String linkman, String telephone,
                               String address, String ly, double price, Integer value, String payStyle, HttpServletResponse response) {
        OrderInfoEntity orderInfoEntity = new OrderInfoEntity();
        orderInfoEntity.setGoodsId(goodsId);
        orderInfoEntity.setUserId(userId);
        orderInfoEntity.setAllmoney(allmoney);
        orderInfoEntity.setOrderStatus(orderStatus);
        orderInfoEntity.setLinkman(linkman);
        orderInfoEntity.setTelephone(telephone);
        orderInfoEntity.setAddress(address);
        orderInfoEntity.setLy(ly);
        orderInfoEntity.setPrice(price);
        orderInfoEntity.setValue(value);
        orderInfoEntity.setPayStyle(payStyle);
        orderInfoEntity.setOrderNumber(OrderCodeFactory.getOrderCode(Long.valueOf(userId)));
        orderService.addOrderInfo(orderInfoEntity);
        orderService.reduceAmount(value, goodsId);
        return ajaxReturn(response, 1, "success", null);
    }

    @GetMapping("get_order_info")
    public String getOrderInfo(Integer userId, Integer type, HttpServletResponse response) {
        List<Map> list = orderService.getOrderInfo(userId, type);
        return ajaxReturn(response, 1, "success", list);
    }

    @GetMapping("get_order_details")
    public String getOrderDetails(String order_number, HttpServletResponse response) {
        List<Map> list = orderService.getOrderDetails(order_number);
        return ajaxReturn(response, 1, "success", list);
    }

}
